function main(subjID, grp, session, TDSFile)

global CURSOR_DURING_TRIAL CURSOR_AFTER_TRIAL KEY_DURING_TRIAL KEY_AFTER_TRIAL SCOREFLAG TEXTFLAG OUTOFBOUNDSALLOWED LETTERFLAG  Key_feedback NO_FB_TRIAL
global A OFFSET XCOORD YCOORD IND_IN_KEY IND_OUT_KEY  subj_no
global hfig
global glovedata T_IN_KEY T_OUT_KEY MT TARG_KEYS PUSH_KEYS SCORE_RECORD
global closeflag A_AMA TARGETS TARGETSx TARGETSy TARGETSxy A_mult
global m n fontsize scrambled
global N_TARGETS
global SQSIZE sqedge  goodsound badsound SOUND_FLAG
global pK pR pF
global schedTrn schedPos schedFB nTrial
global fLogTraj fLogTrial
global logHit logXc logYc logXt logYt logMT logST logTask

disp(subjID);
disp(grp);
disp(session);
disp(TDSFile);

% Set the log directory for the subject.
dirLog = ['log-' subjID];
if ~exist(dirLog)
    mkdir(dirLog);
end
fLogTraj = sprintf('%s/S%s-G%02d-%02d-Traj.mat', dirLog, subjID, grp, session);
fLogTrial = sprintf('%s/S%s-G%02d-%02d-Trial.mat', dirLog, subjID, grp, session);

% Load the TDS file of the subject
load(TDSFile);
parameters;
[schedTrn, schedPos, schedFB, nTrial] = setSched(subjID, grp, session, dirLog);

% Create variables for storing log data
logHit = zeros(nTrial,1);
logXc = zeros(nTrial,1);
logYc = zeros(nTrial,1);
logXt = zeros(nTrial,1);
logYt = zeros(nTrial,1);
logMT = zeros(nTrial,1);
logST = zeros(nTrial,1);
logTask = zeros(nTrial,1);

try
    %Start Simulation
    sim('MyGlove_WithKeyboard')
    
    %Wait Until Message Box OK is Pressed
    uiwait(msgbox('Trials Are Done','End','modal'));
catch
    uiwait(msgbox('Error! Check Hardware Connections or if Save Directory Exists','Error','modal'));
    disp('Terminating...Done');
end
